<?php

class blogplugin extends WikiPlugin
{

	function blogplugin()
	{
		$this->add_macro('blog', 'blog');
		$this->add_macro('commentform', 'commentform');
	}
	
	function blog()
	{
		global $wiki, $mwconfig, $db;
	
		$params = func_get_args();
	
		$dirname = $params[0];
		$cachename = "blog-" . md5("{$dirname}," . implode(',',$params));
		$out = $wiki->get_cache($cachename);
		if($out == '') {
			$limit = isset($params[2])?$params[2]:'10';
			$out = '';
			if($pages = $db->get_results("SELECT id, pagename, postdate FROM {$mwconfig->db_tableprefix}pages WHERE (pagename LIKE '{$dirname}/%') GROUP BY pagename ORDER BY postdate DESC LIMIT {$limit};")) {
				foreach((array)$pages as $page) {
					$nw = new wikiengine();
					$pageout = $nw->getpage("pagename={$page->pagename}&useformat={$params[1]}", $pagerow);
					$wiki->prep_directives[] = $page;
					$out .= $pageout;
				}
			}
			$wiki->add_cache($cachename, array($dirname, $params[1]), $out);
		}
		return $out;
	}
	
	function comments()
	{
	}
	
	function commentform($formpage)
	{
		$out = '<form onsubmit="alert(\'caught\');return false;" class="commentform">';
		$nw = new wikiengine();
		$pageout = $nw->getpage("pagename={$formpage}", $pagerow);
		$out .= $pageout;
		$out .= '</form>';
		return $out;
	}
}

?>
